package com.example;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EurekaTest1 {

	public static final String orderServiceUrl = "http://vm1:9094/testproduct/getProductList";
	public static final AtomicInteger ai = new AtomicInteger(0);

	public static void main(String[] args) {
		int thread = 20;
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(thread);
		for (int i = 0; i < thread; i++) {
			newFixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					doTask();
				}
			});
		}

		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if (ai.get() % 100 == 0) {
						log.info("succ count:{}", ai.get());
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}, "showSuccCount").start();

	}

	private static void doTask() {
		while (true) {
			try {
				HttpClient client = HttpClientBuilder.create().build();
				HttpGet httpget = new HttpGet(orderServiceUrl);
				HttpResponse response = client.execute(httpget);
				String content = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
				if (200 == response.getStatusLine().getStatusCode()) {
					ai.incrementAndGet();
				} else {
					System.err.println(new Date());
					System.err.println(response.getStatusLine().getStatusCode());
					System.err.println(content);
					System.err.println("----------------------------------------------------------");
				}
				Thread.sleep(100);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
